#pragma once
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#include <strings.h>
#include <cstring>
#include <iostream>
#include "Log.hpp"
#include "Common.hpp"

namespace UdpSocketModule
{
    using namespace LogModule;

    const static uint16_t defaultport = 8888;
    class UdpSocket
    {
    private:
        void PrintMessage(std::string message)
        {
            LOG(LogLevel::INFO) << "--------server recvfrom-------";
            LOG(LogLevel::INFO) << message;
            LOG(LogLevel::INFO) << "------------------------------";
        }
    public:
        UdpSocket()
        {
            _socket = socket(AF_INET, SOCK_DGRAM, 0);
            if(_socket < 0)
            {
                LOG(LogLevel::FATAL) << "server socket error!";
                exit(SOCKET_ERR);
            }
            LOG(LogLevel::INFO) << "server socket success! socket: " << _socket;
        }

        void Bind(std::string ip, uint16_t port)
        {
            sockaddr_in local;
            bzero(&local, sizeof(local));
            local.sin_addr.s_addr = inet_addr(ip.c_str());
            local.sin_family = AF_INET;
            local.sin_port = htons(port);
            int n = ::bind(_socket, (sockaddr*)&local, sizeof(local));
            if(n < 0)
            {
                    LOG(LogLevel::FATAL) << "server bind error: " << strerror(errno);
                exit(BIND_ERR);
            }
            LOG(LogLevel::FATAL) << "server bind success!";
        }

        void Recvfrom(std::string* msg, uint16_t* port = nullptr, std::string* ip = nullptr)
        {
            char buffer[1024];
            sockaddr_in peer;
            bzero(&peer, sizeof(peer));
            socklen_t len = sizeof(peer);
            ssize_t n = recvfrom(_socket, buffer, sizeof(buffer) - 1, 0, 
            (sockaddr*)&peer, &len);
            if(n < 0)
            {
                LOG(LogLevel::FATAL) << "server recvfrom error";
                exit(RECVFROM_ERR);
            }
            LOG(LogLevel::INFO) << "server recvfrom success!";
            PrintMessage(buffer);
            msg->assign(buffer, n); //将读到的数组写道msg中
            if(ip != nullptr)
            {
                *ip = inet_ntoa(peer.sin_addr);
            }
            if(port != nullptr)
            {
                *port = htons(peer.sin_port);
            }
        }

        void Sendto(std::string &msg, uint16_t& port, std::string& ip)
        {
            sockaddr_in client;
            memset(&client, 0, sizeof(client));
            client.sin_addr.s_addr = inet_addr(ip.c_str());
            client.sin_family = AF_INET;
            client.sin_port = htons(port);
            ssize_t n = ::sendto(_socket, msg.c_str(), msg.size(), 0, 
            (sockaddr*)&client, sizeof(client));
            if(n < 0)
            {
                LOG(LogLevel::FATAL) << "server Sendto error : " << strerror(errno);
                exit(SENDTO_ERR);
            }
            LOG(LogLevel::INFO) << "server sendto success!";
        }


    private:
        int _socket;
    };
}